package bishi.xunfei;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/08/15 19:33
 */
public class two {
    public static void main(String[] args) {
        System.out.println(findInterval("abcdefghijklmnopqrstuvw???xyz"));
    }

    public static int findInterval(String str) {
        // write code here
        int[] nums = new int[26];
        int now = 0;
        int k = 0;
        int left = 0, right;
        int res = Integer.MAX_VALUE;
        char[] chars = str.toCharArray();
        for (right = 0; right < chars.length; right++) {
            if (chars[right] == '?') {
                k++;
            } else if (right != left && chars[right] == chars[left]) {
                left++;
            } else {
                if (nums[chars[right] - 'a'] == 0) now++;
                nums[chars[right] - 'a']++;
            }
            if (k + now == 26) {
                res = Math.min(right - left + 1, res);

                while (left < right && chars[left] != '?') {
                    if (chars[left] == '?') {
                        if (right - left + 1 >= res) {
                            left++;
                            k--;
                        } else break;
                    } else if (nums[chars[left] - 'a'] > 1) {
                        nums[chars[left] - 'a']--;
                        left++;
                        now--;
                    } else {
                        break;
                    }
                }
                if (right - left + 1 == res) {
                    nums[chars[left] - 'a']--;
                    left++;
                    now--;
                }

            }
        }
        return res;
    }
}
